//
//  UIButton+ZZZCategory.m
//  zjhealth
//
//  Created by 张润东 on 2017/11/30.
//  Copyright © 2017年 amu. All rights reserved.
//

#import "UIButton+ZZZCategory.h"

@implementation UIButton (ZZZCategory)

/**
 自定义默认btn
 */
+(UIButton*)buttonWithTitle:(NSString*)title font:(UIFont*)font color:(UIColor*)color backgroundColor:(UIColor*)backColor image:(id)image cornerRadius:(CGFloat)radius target:(id)target method:(SEL)method
{
    
    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
    //标题
    if (title.length) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    
    //字体大小
    if (font) {
        [button.titleLabel setFont:font];
    }
    
    if (color) {
        [button setTitleColor:color forState:UIControlStateNormal];
    }
    
    //btn图片
    if([image isKindOfClass:[NSString class]]){
        [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    }else if ([image isKindOfClass:[UIImage class]]){
        [button setImage:image forState:UIControlStateNormal];
    }
    
    //背景颜色
    if(backColor){
        [button setBackgroundColor:color];
    }
    
    //圆角
    if (radius) {
        button.layer.cornerRadius=radius;
        button.layer.masksToBounds=YES;
    }
    
    //点击事件
    if (target && method) {
        [button addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
    }
    
   
    return button;
}

-(void)selectedStatus_title:(NSString*)title titleColor:(UIColor*)titleColor image:(id)image{
    
    //标题
    if (title.length) {
        [self setTitle:title forState:UIControlStateSelected];
    }
    
    
    if (titleColor) {
        [self setTitleColor:titleColor forState:UIControlStateSelected];
    }
    
    //btn图片
    if([image isKindOfClass:[NSString class]]){
        [self setImage:[UIImage imageNamed:image] forState:UIControlStateSelected];
    }else if ([image isKindOfClass:[UIImage class]]){
        [self setImage:image forState:UIControlStateSelected];
    }
    
}



//倒计时
-(void)timeDisCount:(int)seconds{
    
    self.userInteractionEnabled=NO;
    
    __block int timeOut =seconds-1;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //设置间隔时间
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
    //timer回调
    dispatch_source_set_event_handler(_timer, ^{
        if (timeOut<=0) {
            //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setTitle:@"获取验证码" forState:UIControlStateNormal];
                self.titleLabel.font = [UIFont systemFontOfSize:13];
                self.userInteractionEnabled = YES;
                [self setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                
            });
        }else
        {
            int second = timeOut%60;
            NSString*strtime = [NSString stringWithFormat:@"%.2d",second];
            //更新ui
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setTitle:[NSString stringWithFormat:@"%@s后重发",strtime] forState:UIControlStateNormal];
                [self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            });
            timeOut--;
            
        }
    });
    dispatch_resume(_timer);//开始计时
}

- (void)layoutWithStyle:(LayoutStyle)style space:(CGFloat)space {
    //1、得到imageView和titleLabel的宽、高
    CGFloat imageWidth = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0;
    CGFloat labelHeight = 0;
    if([UIDevice currentDevice].systemVersion.floatValue >=8.0)
    {
        //由于iOS8中titleLabel的size为0，分开设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    }
    else
    {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    //2、声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    //3、根据style和space设置
    switch (style) {
        case LayoutStyleImageTop:
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageHeight-space/2.0, 0);
            break;
        case LayoutStyleImageLeft:
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, space/2.0);
            break;
        case LayoutStyleImageBottom:
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, -imageWidth, 0);
            break;
        case LayoutStyleImageRight:
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth - space/2.0, 0, imageWidth+space/2.0);
            break;
            
        default:
            break;
    }
    
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
    
}


@end
